
package com.joysuch.wwyt.bp.controller;

import java.text.ParseException;
import java.util.List;

import com.joysuch.wwyt.bp.bean.BpCertificateAddBean;
import com.joysuch.wwyt.bp.bean.BpCertificateAddListBean;
import com.joysuch.wwyt.bp.bean.BpCertificateListBean;
import com.joysuch.wwyt.bp.service.BpCertificateService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "证件管理")
@RequestMapping("/bp/certificate")
public class BpCertificateController {

    @Autowired
    private BpCertificateService certificateService;

    @GetMapping("getPage")
    public ResultBean page(Long userId) {
        List<BpCertificateListBean> page = certificateService.getPage(userId);
        return ResultBean.wrapSuccessfulResult(page);
    }

    @PostMapping("add")
    public ResultBean add(@RequestBody BpCertificateAddBean bpCertificate) throws ParseException {
        BpCertificateListBean add = certificateService.add(bpCertificate);
        System.out.println(ToStringBuilder.reflectionToString(add));
        return ResultBean.wrapSuccessfulResult(add);
    }

    @PostMapping("update")
    public ResultBean edit(@RequestBody BpCertificateAddBean bpCertificate) throws Exception {
        Assert.notNull(bpCertificate.getId(), "ID不能为空");
        BpCertificateListBean add = certificateService.update(bpCertificate);
        System.out.println(ToStringBuilder.reflectionToString(add));
        return ResultBean.wrapSuccessfulResult(add);
    }

    @PostMapping("delete")
    public ResultBean delete(Long id) {
        certificateService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("getById")
    public ResultBean getById(Long id) {
        BpCertificateAddListBean byId = certificateService.getById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }

}
